package recommend

import (
	"context"
	"math/rand"
	"strconv"

	"mall/common/globalkey"
	"mall/service/backend/api/internal/svc"
	"mall/service/backend/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type InitUserFeedbackLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 初始Gorse反馈
func NewInitUserFeedbackLogic(ctx context.Context, svcCtx *svc.ServiceContext) *InitUserFeedbackLogic {
	return &InitUserFeedbackLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *InitUserFeedbackLogic) InitUserFeedback() (*types.BackendDiagnoseResp, error) {
	var feedback = []string{
		globalkey.RecommendedFeedBackTypeRead,
		globalkey.RecommendedFeedBackTypeLike,
		globalkey.RecommendedFeedBackTypeWantHave,
		globalkey.RecommendedFeedBackTypeHave,
		globalkey.RecommendedFeedBackTypeCollect,
	}
	for i := 1; i <= 10; i++ {
		for u := 1; u <= 50; u++ {
			randomInt := rand.Intn(len(feedback))
			err := l.CreateFeedbackLogic(globalkey.GorseKnowledgeBasePrefix+strconv.FormatInt(int64(i), 10), strconv.FormatInt(int64(u), 10), feedback[randomInt])
			if err != nil {
				return nil, err
			}
			randomInt1 := rand.Intn(len(feedback))
			err = l.CreateFeedbackLogic(globalkey.GorseBLogPostPrefix+strconv.FormatInt(int64(i), 10), strconv.FormatInt(int64(u), 10), feedback[randomInt1])
			if err != nil {
				return nil, err
			}
		}
	}
	return nil, nil
}

func (l *InitUserFeedbackLogic) CreateFeedbackLogic(pid, uid string, fType string) error {
	return l.svcCtx.GorseClient.CreateFeedback(
		l.svcCtx.Config.Query.GorseUrl,
		pid, uid,
		fType,
	)
}
